package com.mall507.fivepeaks.activity.community;

import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.hzh.frame.comn.annotation.ViewInject;
import com.hzh.frame.ui.activity.AbsRecyclerViewUI;
import com.hzh.frame.widget.xrecyclerview.RecyclerViewHolder;
import com.hzh.frame.widget.xrefresh.XSwipeRefreshLayout;
import com.mall507.comn.model.AssociationBussinessStore;
import com.mall507.fivepeaks.R;
import com.mall507.fivepeaks.activity.main.MainTourRUI;
import com.mall507.fivepeaks.widget.imageview.ExpandImageView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 五峰特产
 */
@Route(path = "/community/CommunityAssociationBussinessStoreRUI")
public class CommunityAssociationBussinessStoreRUI extends AbsRecyclerViewUI<AssociationBussinessStore> {

    @Override
    protected int setLayoutId() {
        return R.layout.ui_rv_community_bussiness;
    }

    @Override
    protected void bindView() {
        showLoding();
        getTitleView().setContent("分类列表");
    }
    
    @Override
    protected String setHttpPath() {
        return "Business/getBusinessList";
    }

    @Override
    protected JSONObject setHttpParams() {
        JSONObject params = new JSONObject();
        try {
            params.put("type", getIntent().getStringExtra("type"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return params;
    }

    @Override
    protected List<AssociationBussinessStore> handleHttpData(JSONObject response) {
        List<AssociationBussinessStore> list = new ArrayList<AssociationBussinessStore>();
        if (200 == response.optInt("code")) {
            JSONArray listJson = response.optJSONArray("data");
            if (listJson != null && listJson.length() > 0) {
                for (int i = 0; i < listJson.length(); i++) {
                    JSONObject obj = listJson.optJSONObject(i);
                    AssociationBussinessStore model = new AssociationBussinessStore();
                    model.setBid(obj.optString("bid"));
                    model.setThumb(obj.optString("thumb"));
                    model.setCompname(obj.optString("compname"));
                    model.setCompaddress(obj.optString("compaddress"));
                    model.setCollection_count(obj.optString("collection_count"));
                    model.setComment_count(obj.optString("comment_count"));
                    list.add(model);
                }
            }
        }
        dismissLoding();
        return list;
    }

    @Override
    protected void handleHttpDataFailure() {
        super.handleHttpDataFailure();
        showLodingFail();
    }

    @Override
    protected int setItemLayoutId(int viewType) {
        return R.layout.item_rv_community_business;
    }

    @Override
    protected void bindItemData(RecyclerViewHolder holder, int position, final AssociationBussinessStore model) {
        ((ExpandImageView) holder.getView(R.id.icon)).setImageURI(model.getThumb());
        holder.setText(R.id.title, model.getCompname());
        holder.setText(R.id.desc, "地址" + model.getCompaddress());
        holder.setText(R.id.collectionNumber,model.getCollection_count() + "收藏");
        holder.setText(R.id.commentNumber,model.getComment_count() + "评论");
        holder.setOnItemClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ARouter.getInstance().build("/community/CommunityBusinessStoreInfoRUI")
                        .withString("id", model.getBid())
                        .withString("type","3")
                        .withString("collection_count",model.getCollection_count())
                        .navigation();
            }
        });
    }

    @Override
    protected void onRestart() {
        onRefresh();
        super.onRestart();
    }
}
